package com.fanfanwang.ffw.app.asynctask;

import java.util.LinkedHashMap;

import org.json.JSONException;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.bean.SucGetAppVersionBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.fragment.MainMyFragment;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;

public class ChkVersionAsyncTask extends
		IssAsyncTask<String, String, SucGetAppVersionBean> {
	private Activity mActivity;

	public ChkVersionAsyncTask(Activity activity) {
		super(activity);
		mActivity = activity;
	}

	@Override
	protected SucGetAppVersionBean doInBackground(String... params) {
		SucGetAppVersionBean versionSucBean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("system", "android");
		paramsMap.put("type", "0");
		// type 0为用户端 1为商户端

		try {
			versionSucBean = IssNetLib.getInstance(mActivity).getAppVersion(
					WebServiceHelper._GETAPPVERSION, paramsMap);
		} catch (HttpRequestException e) {
			e.printStackTrace();
			if (mActivity != null) {
				// exception = ((ContextThemeWrapper)
				// mActivity).getResources().getString(R.string.e_net_error);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionSucBean;
	}

	@Override
	protected void onPostExecute(SucGetAppVersionBean result) {
		super.onPostExecute(result);
		if (exception != null) {
			if (mActivity != null)
				ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {
				if (MainMyFragment.iChkVersion != null && result.data != null) {
					MainMyFragment.iChkVersion.setVersion(result.data, mActivity);
				}
			} else if (result.status == 0) {
				if (!TextUtils.isEmpty(result.msg)) {
					ToastAlone.showToast(mActivity,
							AppConstant.URLDecoderString(result.msg),
							Toast.LENGTH_SHORT).show();
				} else {
					ToastAlone.showToast(
							mActivity,
							mActivity.getResources().getString(
									R.string.e_net_error)).show();
				}
			}
		}
	}

}
